﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Security;

namespace Moving03.Services
{
    /// <summary>
    /// Summary description for DatVanChuyen
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class DatVanChuyen : System.Web.Services.WebService
    {
        DataClasses1DataContext context = new DataClasses1DataContext();
        Guid idUser = new Guid();
        public Guid Lay_IDUser(string username)
        {
            var user = context.aspnet_Users.Single(u => u.UserName == username);
            if (user != null)
            {
                return user.UserId;
            }else
                return Guid.Empty;
        }
        [WebMethod(EnableSession = true)]
        public bool CheckLogin(string username, string password)
        {
            bool retVal = Membership.ValidateUser(username, password);
            if (retVal == true)
            {
                idUser = Lay_IDUser(username);
                Application.Add("idUser", idUser);
            }
            else
                Application.Add("idUser", "-1");
            return retVal;
        }
        [WebMethod]
        public int InsertNoiDi(string HoTen, int QuocGia, string DiaChi, string ThanhPho, int MaBuuDien, string Email, string DienThoai)
        {
            if ((object)Application["idUser"] == null || Application["idUser"].ToString() == "-1")
            {
                return -1;
            }
            else
            {
                try
                {
                    NoiDi ndi = new NoiDi();
                    ndi.NguoiGoi = HoTen;
                    ndi.Id_Quocgia = QuocGia;
                    ndi.Diachi = DiaChi;
                    ndi.Thanhpho = ThanhPho;
                    ndi.Mabuudien = MaBuuDien;
                    ndi.Email = Email;
                    ndi.Dienthoai = DienThoai;
                    context.NoiDis.InsertOnSubmit(ndi);
                    context.SubmitChanges();
                    Application.Add("idNoiDi", ndi.Id_Noidi);
                    return ndi.Id_Noidi;
                }
                catch
                {
                    throw new Exception();
                }
            }
        }
        [WebMethod]
        public int InsertLoHang(string GiongHayKhac, string TenMatHang, string TenLoHang, int SoGoi, double TrongLuongMoiGoi, int GiaTriLoHang, double TongKhoiLuong)
        {
            // insert bảng Lô hàng
             // insert Noiden
            if ((object)Application["idUser"] == null || Application["idUser"].ToString() == "-1")
            {
                return -1;
            }
            else
            {
                LoHang lh = new LoHang();
                if (GiongHayKhac == "1")
                {
                    lh.Giong_nhau = 1;

                    MatHang matHang = new MatHang();
                    matHang.TenMatHang = TenMatHang;
                    matHang.TrongLuong = TrongLuongMoiGoi;
                    matHang.TriGia = GiaTriLoHang;
                    context.MatHangs.InsertOnSubmit(matHang);
                    context.SubmitChanges();
                }
                else
                    lh.Giong_nhau = 0;

                lh.Ten_lo_hang = TenLoHang;
                lh.So_goi = SoGoi;
                lh.Gia_tri = GiaTriLoHang;
                lh.TongTrong_luong = TongKhoiLuong;

                context.LoHangs.InsertOnSubmit(lh);
                context.SubmitChanges();
                return lh.Id_LoHang;
            }
        }
        [WebMethod(EnableSession = true)]
        public int InsertNoiDen(string Email, string DienThoai, string DiaChi1, string DiaChi2, int MaQuocGia, int MaBuuDien, string NguoiNhan, string ThanhPho, int NhaRieng)
        {
            // insert Noiden
            if ((object)Application["idUser"] == null || Application["idUser"].ToString() == "-1")
            {
                return -1;
            }
            else
            {

                NoiDen nd = new NoiDen();
                nd.Email = Email;
                nd.Dienthoai = DienThoai;
                nd.Diachi1 = DiaChi1;
                nd.Diachi2 = DiaChi2;
                nd.Id_Quocgia = MaQuocGia;

                nd.Mabuudien = MaBuuDien;
                nd.Nguoinhan = NguoiNhan;
                nd.Thanhpho = ThanhPho;

                nd.Nharieng = NhaRieng;
                nd.IdUser = (Guid)Application["idUser"];
                context.NoiDens.InsertOnSubmit(nd);
                context.SubmitChanges();
                Application.Add("idNoiDen", nd.Id_NoiDen);
                return nd.Id_NoiDen;
            }
            return 0;
        }
        [WebMethod]
        public int InsertMatHang(int id_LoHang, string TenMatHang, float TrongLuong, float TriGia)
        {
            if ((object)Application["idUser"] == null || Application["idUser"].ToString() == "-1")
            {
                return -1;
            }
            else
            {
                // insert bảng MatHang           
                MatHang mh = new MatHang();
                mh.Id_LoHang = id_LoHang;
                mh.TenMatHang = TenMatHang;
                mh.TrongLuong = TrongLuong;
                mh.TriGia = TriGia;
                context.MatHangs.InsertOnSubmit(mh);
                context.SubmitChanges();
            }
            return 0;
        }
        [WebMethod]
        public int InsertThongTinVanChuyen(int id_LoHang, int PhuongThucThanhToan, int PhuongThucGuiHang, string BiMat1, string BiMat2, string TenDoanhNghiep, string TenLienLac, string TenLienLacSuCo, string DiaChiTraHang, int DungKiGui, int ThongBaoQuaEmail, int TacDongKhiHau)
        {
            if ((object)Application["idUser"] == null || Application["idUser"].ToString() == "-1")
            {
                return -1;
            }
            else
            {
                try
                {
                    ThongTinVanChuyen ttvc = new ThongTinVanChuyen();
                    ttvc.Id_PhuongThucThanhToan = PhuongThucThanhToan;
                    ttvc.Id_PhuongThucGoiHang = PhuongThucGuiHang;
                    ttvc.ThongTinBiMat1 = BiMat1;
                    ttvc.ThongTinBiMat2 = BiMat2;
                    ttvc.Id_LoHang = id_LoHang;
                    ttvc.TenDoanhNghiep = TenDoanhNghiep;
                    ttvc.TenLienLac = TenLienLac;
                    ttvc.IdNoiden = (int)Application["idNoiDen"];
                    ttvc.IdNoiDi = (int)Application["idNoiDi"];
                    ttvc.TenNeuSuCo = TenLienLacSuCo;
                    ttvc.DiaChiGoiNeuSuCo = DiaChiTraHang;

                    ttvc.DungKiGoi = DungKiGui;
                    ttvc.NhanEmail = ThongBaoQuaEmail;
                    ttvc.TacDongKhiHau = TacDongKhiHau;
                    ttvc.TinhTrang = 1;
                    ttvc.NgayDat = DateTime.Now;

                    ttvc.Id_User = (Guid)Application["idUser"];
                    context.ThongTinVanChuyens.InsertOnSubmit(ttvc);
                    context.SubmitChanges();
                    int maVanChuyen = ttvc.Id_TTVC;
                    int id_ddcn = insertDiaDiemCapNhat(maVanChuyen);
                    insertDuongDiChoLoHang(maVanChuyen, id_ddcn);
                    return 0;
                }
                catch (Exception ex)
                {
                    throw;
                }
            }
        }
        public void insertDuongDiChoLoHang(int id_TTVC, int id_ddcn)
        {
            int id_noidi = 0;
            int id_noiden = 0;
            var query = from ttvc in context.ThongTinVanChuyens
                        join noidi in context.NoiDis on ttvc.IdNoiDi equals noidi.Id_Noidi
                        join noiden in context.NoiDens on ttvc.IdNoiden equals noiden.Id_NoiDen
                        where ttvc.Id_TTVC == id_TTVC
                        select new { id_noidi = noidi.Id_Quocgia, id_noiden = noiden.Id_Quocgia };


            foreach (var thongtin in query)
            {
                id_noidi = (int)thongtin.id_noidi;
                id_noiden = (int)thongtin.id_noiden;
            }

            var query1 = from cpddnn in context.ChiPhiDiaDiemNuocNgoais
                         join ddtg1t in context.DiaDiemTrungGian1Tuyens on cpddnn.Id_CPDDNNgoai equals ddtg1t.Id_CPDDNNgoai
                         join dd in context.DiaDiemTrungGians on ddtg1t.Id_TG equals dd.Id_DDTrungGian
                         where cpddnn.Id_QuocGia1 == id_noidi && cpddnn.Id_QuocGia2 == id_noiden
                         select dd;

            foreach (var dd in query1)
            {
                DuongDi duongdi = new DuongDi();
                duongdi.DaToiChua = 0;
                duongdi.Id_DDTGian = dd.Id_DDTrungGian;
                duongdi.Id_DDCapNhat = id_ddcn;
                context.DuongDis.InsertOnSubmit(duongdi);
                context.SubmitChanges();

            }

        }

        private int insertDiaDiemCapNhat(int id_TTVC)
        {
            DiaDiemCapNhat ddcn = new DiaDiemCapNhat();
            ddcn.Id_ThongTinVanChuyen = id_TTVC;
            context.DiaDiemCapNhats.InsertOnSubmit(ddcn);
            context.SubmitChanges();
            int id_diadiemcapnhat = ddcn.Id_DDCapNHat;
            return id_diadiemcapnhat;
        }
    }
}
